function [nrec] = thist2ati(xmin,xmax)

%  Initially copied from thist2unv on 12/21/04 by B. Ross
%  This function will create an IDEAS ATI file containing time history
%  records from the Project structure.  The function will prompt for
%  which channels to export to the ati file.  This function uses
%  the IMAT routines to create the ATI file.
%
%  **NOTE**: If the user wishes to append a file, they must answer YES
%  to the OVERWRITE prompt the system generates when they select the file.
%  This function will then generate another prompt asking whether to append
%  or overwrite.  This is an issue with using the UIPUTFILE function.
%
%  On 1/11/05 cleaned up some comments and fixed problem with use of xmin
%  and xmax.
%
%  Last modified on 1/11/05 by B. Ross
%
%  ************************************************************************
%  [num_records_written] = thist2ati(xmin,xmax)
%
%  num_records_written = number of records(channels) written to ATI file
%                 xmin = export start time selected in main Shell window
%                 xmax = export end time selected in main Shell window
%
% **************************************************************************

global Project;

nrec = 0;

%  Select Channels to Export

[fchan, lchan, varargout] = channel_select;

%  Prompt for ATI output filename

[fname,pname] = uiputfile([Project.Test_Item.Run.Setup_File '.ati'],...
                    'Select Export ATI File');
if isequal([fname, pname], [0,0])
    return
end
filename = fullfile(pname, fname);

%  Create ideas_fn object with default header info

f = ideas_fn(lchan-fchan+1);

%  Replace default header fields with our specific data

%  Common to all channels in Project Time Histories
f.IDLine1 = Project.Test_Item.Run.Descrip;
f.IDLine2 = Project.Test_Item.Run.Test_Date;
f.AbscissaMin = xmin;
f.AbscissaInc = 1/Project.Test_Item.Run.Sample_Rate;
f.FunctionType = 'Time Response'; % Time Response
f.AbscissaDataType = 'Time'; % Time

%  For each channel selected ...

for i=fchan:lchan
    
    % Correct for missing data and bad channels
    if isempty(Project.Test_Item.Run.Channel(i).Direction)
        Project.Test_Item.Run.Channel(i).Direction = 'NA';
    end
    if isempty(str2num(Project.Test_Item.Run.Channel(i).Coordinate))
        Project.Test_Item.Run.Channel(i).Coordinate = num2str(9999);
    end
    
    temp = [Project.Test_Item.Run.Channel(i).Direction ...
            Project.Test_Item.Run.Channel(i).Orientation];
    f(i-fchan+1).ResponseNode = str2num(Project.Test_Item.Run.Channel(i).Coordinate);
    f(i-fchan+1).ReferenceNode = str2num(Project.Test_Item.Run.Channel(i).Coordinate);
    f(i-fchan+1).ResponseDir = temp;
    f(i-fchan+1).ReferenceDir = temp;
    f(i-fchan+1).Ordinate=double(Project.Test_Item.Run.Channel(i).Time_His(1,xmin:xmax)); 
    f(i-fchan+1).ResponseEntity = Project.Test_Item.Run.Channel(i).Trans_Loc;
    if Project.Test_Item.Run.Channel(i).EU == 'g'
        f(i-fchan+1).OrdNumDataType = 12; %acceleration
    elseif Project.Test_Item.Run.Channel(i).EU == 'lbs'
        f(i-fchan+1).OrdNumDataType = 13; %excitation force
    else
        f(i-fchan+1).OrdNumDataType = 0; %unknown
    end

    nrec = nrec + 1;
end

%  Actually write the information to a file
setunits;
writeadf(filename,f);

message = sprintf('%d records of data written',nrec);
disp(message)

